package org.angelica.modules.account.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import org.angelica.common.query.BaseQuery;
import org.angelica.common.wrapper.ResponseWrapper;
import org.angelica.core.response.ResponseResult;
import org.angelica.modules.account.entity.AccPermissionDO;
import org.angelica.modules.account.form.AccPermissionCreateForm;
import org.angelica.modules.account.form.AccPermissionUpdateForm;
import org.angelica.modules.account.service.AccPermissionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author aizhimin
 * @since 2019-05-13
 */
@RestController
@RequestMapping("api/account/accPermission")
public class AccPermissionController {

    @Autowired
    private AccPermissionService accPermissionService;

    @GetMapping("/")
    public ResponseResult listAdminPermission(BaseQuery baseQuery){
        IPage<AccPermissionDO> accPermissionPage = accPermissionService.listPermission(baseQuery);
        return ResponseResult.success("data", ResponseWrapper.wrapperPage(accPermissionPage));
    }

    /**
     * 页面初始化数据
     * @return
     */
    private Map<String,Object> initData(){
        BaseQuery baseQuery = new BaseQuery();
        IPage<AccPermissionDO> accPermissionPage = accPermissionService.listPermission(baseQuery);
        return ResponseWrapper.wrapperPage(accPermissionPage);
    }

    @GetMapping("/listAll")
    public ResponseResult listAllAccPermission(){
        List<AccPermissionDO> accPermissionList = accPermissionService.listTreeData();
        return ResponseResult.success("data", accPermissionList);
    }

    @PostMapping("/create")
    public ResponseResult createAccPermission(@RequestBody @Valid AccPermissionCreateForm accPermissionCreateForm){
        AccPermissionDO accPermissionDO = AccPermissionDO.builder().build();
        BeanUtils.copyProperties(accPermissionCreateForm,accPermissionDO);
        accPermissionDO.setGmtCreate(Calendar.getInstance().getTime());
        accPermissionService.save(accPermissionDO);
        return ResponseResult.success("data",initData());
    }

    @PostMapping("/update")
    public ResponseResult updateAccPermission(@RequestBody @Valid AccPermissionUpdateForm accPermissionUpdateForm){
        AccPermissionDO accPermissionDO = AccPermissionDO.builder().build();
        BeanUtils.copyProperties(accPermissionUpdateForm,accPermissionDO);
        accPermissionDO.setGmtModified(Calendar.getInstance().getTime());
        accPermissionService.updateById(accPermissionDO);
        return ResponseResult.success("data",initData());
    }

    @GetMapping("/delete/{id}")
    public ResponseResult deleteAccPermission(@PathVariable(value = "id")Long id){
        accPermissionService.removeById(id);
        return ResponseResult.success("data",initData());
    }
}
